package com.ct.ecommerce.uc.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ct.ecommerce.api.uc.client.UserService;
import com.ct.ecommerce.framework.security.UserWrapperDTO;
import com.ct.ecommerce.uc.dao.entity.UserEntity;
import com.ct.ecommerce.uc.dao.mapper.UserMapper;
import java.util.List;
import java.util.Optional;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author changtong
 * @since 2021/5/31
 */
@DubboService
@Slf4j
public class UserServiceImpl implements UserService {


    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails findUserByName(String name) {

        return Optional.ofNullable(userMapper.selectOne(new LambdaQueryWrapper<UserEntity>()
            .eq(UserEntity::getUsername, name)))
            .map(u -> {
                UserWrapperDTO user = new UserWrapperDTO();
                user.setUserId(u.getId());
                user.setUserName(u.getUsername());
                user.setPassword(u.getPassword());
                List<String> permissionList = userMapper
                    .findUserPermissionByUserId(u.getId());
                user.setPermissionList(permissionList);
                return user;
            }).orElse(null);


    }
}
